package net.huashitong.supervision.task.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysDeptFilter;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.DeptService;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.BriefCategory;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.filter.BriefCategoryFilter;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.supervision.task.service.BriefCategoryService;
import net.huashitong.supervision.task.service.TaskCrucialService;
import net.huashitong.supervision.task.service.TaskMainService;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/taskCrucial")
public class TaskCrucialController extends JsonBaseController {
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private TaskCrucialService taskCrucialService;
    @Autowired
    private DeptService deptService;

    @Override
    protected String getView(String s) {
        return "/modules/task/taskCrucial/" + s;
    }


    @RequestMapping(method = RequestMethod.GET)
    public String initPage(Model model) {
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        if (!"/01/".equals(dept.getDeptRelation())) {
            return null;
        }
        return getView("query");
    }

    @RequestMapping(value = "/inspection",method = RequestMethod.GET)
    public String initInspectionPage(Model model) {
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        if (!"/01/".equals(dept.getDeptRelation())) {
            return null;
        }
        return getView("query2");
    }

    /**
     *  攻坚查看
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid initPageData(TaskMainFilter filter) {
        List<TaskMain> taskMains = new ArrayList<>();
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        String userid= SysUserUtils.getUserId();
        if ("/01/".equals(dept.getDeptRelation())) {
            //督查单位
            if ("02".equals(sysLogin.getRoleId())) {
                // 所有的攻坚 主任务 都是领导看的
                filter.setCrucialUser("ALL");
            } else {
                filter.setCrucialUser(userid);
            }
            //查询攻坚系列
            taskMains = taskMainService.queryFilter(filter);
        }
        //不是督查 不查询
        return coverJqGrid(filter,taskMains);
    }

    /**
     *  督查查看
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/inspection",method = RequestMethod.POST)
    public JqGrid initInspectionPageData(TaskMainFilter filter) {
        List<TaskMain> taskMains = new ArrayList<>();
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        String userid= SysUserUtils.getUserId();
        if ("/01/".equals(dept.getDeptRelation())) {
            //督查单位
            if ("02".equals(sysLogin.getRoleId())) {
                // 所有的攻坚 主任务 都是领导看的
                filter.setInspectionId("ALL");
            } else {
                filter.setInspectionId(dept.getSysId());
            }
            filter.setOverdueDept("ALL");
            //查询攻坚系列
            taskMains = taskMainService.queryFilter(filter);
        }
        //直接返回单位名称
        List<SysDept>  sysDepts = deptService.queryByDeptLevel(3);
        for (TaskMain taskMain:taskMains) {
            String id = taskMain.getOverdueDept();
            for (SysDept sysDept:sysDepts) {
                if (sysDept.getSysId().equals(id)){
                    taskMain.setOverdueDept(sysDept.getDeptName());
                }
            }
        }
        //不是督查 不查询
        return coverJqGrid(filter,taskMains);
    }

    /**
     * 创建待立项
     * @param filter
     * @param model
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute TaskMainFilter filter , Model model) {
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        if (!"/01/".equals(dept.getDeptRelation())) {
            return null;
        }
        //所有督察组
        SysDeptFilter sysDeptFilter = new SysDeptFilter();
        sysDeptFilter.setDeptRelation("/01/");
        List<SysDept> depts = sysDeptService.query(sysDeptFilter);
        List<SysDept> deptForModel = new ArrayList<>();
        for (SysDept sysDept:depts){
            if(!"攻坚作战室".equals(sysDept.getDeptName())&&!"县领导".equals(sysDept.getDeptName())) {
                deptForModel.add(sysDept);
            }
        }
        model.addAttribute("TaskMainFilter",filter);
        model.addAttribute("groups",deptForModel);   //sysId
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute TaskMainFilter filter) {
        //创建人与攻坚人 一样的 就是 待立项任务
        if (StringUtils.isBlank(filter.getInspectionId())){
            return coverMessage(UtilsTools.ERROR);
        } else if(StringUtils.isBlank(filter.getMainName())){
            return coverMessage(UtilsTools.ERROR,"任务名称不可为空");
        } else {
            try {
                taskCrucialService.addTaskCrucial(filter);
            } catch (Exception e) {
                return coverMessage(UtilsTools.ERROR,e.getMessage());
            }
        }
        return coverMessage(UtilsTools.CODE);
    }

    @ResponseBody
    @RequestMapping(value = "/getInspection/{id}", method = RequestMethod.GET)
    public Message getInspection(@PathVariable String id){
        SysDeptFilter sysDeptFilter = new SysDeptFilter();
        sysDeptFilter.setDeptRelation("/01/");
        List<SysDept> depts = sysDeptService.query(sysDeptFilter);
        for (SysDept dept:depts) {
            if (dept.getSysId().equals(id)){
                Map<String,Object> map = new HashMap<>();
                map.put("deptName",dept.getDeptName());
                return coverMessage("200","获取成功",map);
            }
        }
        return coverMessage(UtilsTools.ERROR);
    }

    @ResponseBody
    @RequestMapping(value = "/getUnit/{id}", method = RequestMethod.GET)
    public Message getUnit(@PathVariable String id){
        List<SysDept> depts = deptService.queryByDeptLevel(3);
        for (SysDept dept:depts) {
            if (dept.getSysId().equals(id)){
                Map<String,Object> map = new HashMap<>();
                map.put("deptName",dept.getDeptName());
                return coverMessage("200","获取成功",map);
            }
        }
        return coverMessage(UtilsTools.ERROR);
    }



    @ResponseBody
    @RequestMapping(value = "/getInspectionId", method = RequestMethod.GET)
    public Message getInspectionId(){
        SysDeptFilter sysDeptFilter = new SysDeptFilter();
        sysDeptFilter.setDeptRelation("/01/");
        List<SysDept> sysDepts = sysDeptService.query(sysDeptFilter);
        List<SysDept> deptList = new ArrayList<>();
        for (SysDept sysDept:sysDepts){
            if(!"攻坚作战室".equals(sysDept.getDeptName())&&!"县领导".equals(sysDept.getDeptName())) {
                deptList.add(sysDept);
            }
        }
        return coverMessage("200","获取成功",deptList);
    }


    /**
     * 督查组 创建  待立项  分配给单位
     * 创建待立项
     * @param filter
     * @param model
     * @return
     */
    @RequestMapping(value = "/inspection/create", method = RequestMethod.GET)
    public String showInspectionAddView(@ModelAttribute TaskMainFilter filter , Model model) {
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        if (!"/01/".equals(dept.getDeptRelation())) {
            return null;
        }
        //得到所有单位 的部门
        List<SysDept>  sysDepts = deptService.queryByDeptLevel(3);
        model.addAttribute("TaskMainFilter",filter);
        model.addAttribute("inspectionId",dept.getSysId());
        model.addAttribute("deptName",dept.getDeptName());
        model.addAttribute("units",sysDepts);
        return getView("form2");
    }


    @ResponseBody
    @RequestMapping(value = "/inspection/create", method = RequestMethod.POST)
    public Message inspectionCreate(@ModelAttribute TaskMainFilter filter) {
        //创建人与攻坚人 一样的 就是 待立项任务
        if (StringUtils.isBlank(filter.getInspectionId())||StringUtils.isBlank(filter.getOverdueDept())){
            return coverMessage(UtilsTools.ERROR);
        }  else if(StringUtils.isBlank(filter.getMainName())||StringUtils.isBlank(filter.getContent())){
            return coverMessage(UtilsTools.ERROR,"任务名称和内容不可为空");
        } else {
            try {
                taskCrucialService.addTaskInspection(filter);
            } catch (Exception e) {
                return coverMessage(UtilsTools.ERROR,e.getMessage());
            }
        }
        return coverMessage(UtilsTools.CODE);
    }


}
